package cn.utry.sipGateWay.tcpdump;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.utry.sipGateWay.config.SiriConfig;
import cn.utry.sipGateWay.netty.RtpServer;
import cn.utry.sipGateWay.netty.SipServer;
import cn.utry.sipGateWay.sip.SessionContainer;
import cn.utry.sipGateWay.sip.SipMessageUtil;
import cn.utry.sipGateWay.sip.SipMethod;
import cn.utry.sipGateWay.sip.SipMsgKey;
import cn.utry.sipGateWay.sip.SipSession;
import io.netty.util.internal.StringUtil;
import jpcap.PacketReceiver;
import jpcap.packet.IPPacket;
import jpcap.packet.Packet;
import jpcap.packet.UDPPacket;

public class Receiver implements  PacketReceiver{
	private static final Logger log = LoggerFactory.getLogger(Receiver.class);
	private static final Map<Integer,String> rtpPortMap=new ConcurrentHashMap<Integer, String>();
	private Filter filter;

	public void setFilter(Filter filter) {
		this.filter = filter;
	}


	@Override
	public void receivePacket(Packet packet) {
		// TODO Auto-generated method stub
		if (packet instanceof IPPacket && ((IPPacket) packet).version == 4) {
			if(packet instanceof UDPPacket) {
	            UDPPacket udpPacket = (UDPPacket) packet;
	            String srcIp=udpPacket.src_ip.getHostAddress();
	            int srcPort=udpPacket.src_port;
	            String dstIp=udpPacket.dst_ip.getHostAddress();
	            int dstPort=udpPacket.dst_port;
	            log.info("捕获到的udp包 源地址 {}:{}    目的地址{}:{}",srcIp,srcPort,dstIp,dstPort);
	            String rtpIp=rtpPortMap.get(srcPort);
	            if(StringUtil.isNullOrEmpty(rtpIp)&&rtpIp.equals(srcIp)) {
	            	if(srcIp.equals(filter.getDstIp())) {
	            		
	            	}
	            }else {
	            	if(filter.isRequest(srcIp, dstIp)) {
		            	if(filter.getDstPort()==dstPort) {
		            		String msg=new String(udpPacket.data);
		            		Map<String,Object> map=SipMessageUtil.reciveMsg(msg);
		            		if(map.containsKey(SipMessageUtil.method)) {
			            		if(map.get(SipMessageUtil.method).equals(SipMethod.INVITE)) {
			            			log.info("sip {}请求。。。。 \n {}",SipMethod.INVITE,msg);
		            				int rtpPort=SipMessageUtil.getRtpPort(map);
		            				log.info("需要捕获的rtp流的端口是:{}",rtpPort);
		            				rtpPortMap.put(rtpPort,srcIp);	  			            			
			            			map.put(SipMsgKey.SRC, srcIp);
			            			map.put(SipMsgKey.SRC_PORT, udpPacket.src_port+"");
			            			map.put(SipMsgKey.DST, dstIp);
			            			map.put(SipMsgKey.DST_PORT, dstPort+"");
			            			SipSession session=new SipSession();
			            			session.setCallId(map.get(SipMsgKey.Call_ID)+"");		
			            			session.setInviteMap(map);    
			            			SipServer fromServer=new SipServer();
			            			fromServer.setPort(srcPort+1);
			            			session.setFormServer(fromServer);			            			
			            			RtpServer fromRtpServer=new RtpServer();
			            			fromRtpServer.setPort(rtpPort);
			            			session.setFromRtpServer(fromRtpServer);
          			
			            			SessionContainer.addSession(session);
			            		}
		            		}
		            	}
		            }else {
		            	if(filter.getDstPort()==srcPort) {
		            		String msg=new String(udpPacket.data);
		            		Map<String,Object> map=SipMessageUtil.reciveMsg(msg);
		            		if(map.containsKey(SipMessageUtil.method)) {
		            			if(map.get(SipMessageUtil.method).equals(SipMethod.OK)) {
		            				log.info("sip {}请求。。。。 \n {}",SipMethod.OK,msg);
		            				int rtpPort=SipMessageUtil.getRtpPort(map);
		            				log.info("需要捕获的rtp流的端口是:{}",rtpPort);
		            				rtpPortMap.put(rtpPort,srcIp);		            				
		            				String callId=map.get(SipMsgKey.Call_ID).toString();
		            				SipSession session=SessionContainer.getSession(callId);
		            				SipServer toServer=new SipServer();
		            				toServer.setPort(srcPort+10000);
			            			session.setToServer(toServer);	            			
			            			RtpServer toRtpServer=new RtpServer();
			            			toRtpServer.setPort(rtpPort);
			            			session.setToServer(toServer);	            
		            			}
		            		}
		            	}
		            }
	            }
	            
			}
		}
	}

}
